/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.istia.gesstages.actions;

import com.opensymphony.xwork2.ActionSupport;
import fr.istia.gesstages.dao.IDaoOffer;
import fr.istia.gesstages.jpa.Offer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.commons.beanutils.BeanComparator;

/**
 *
 * @author Gaëtan
 */
@SuppressWarnings("serial")
public class OfferAction extends ActionSupport {

    private IDaoOffer beanDaoOffer;
    private List<Offer> offersList = new ArrayList<Offer>();
    private Offer offer;
    private int id;

    public String deleteOffer() {
        try {
            offer = beanDaoOffer.find(id);
            beanDaoOffer.delete(offer);
            return SUCCESS;
        } catch (Exception ex) {
            return "input";
        }
    }
    
    public String validateEdit() {
        try {
            offer.setArchive(false);
            this.offer = beanDaoOffer.update(offer);
            if (offer == null) {
                return "input";
            } else {
                return SUCCESS;
            }
        } catch (Exception ex) {
            return "input";
        }
    }

    public String findOffer() {
        try {
            this.offer = beanDaoOffer.find(id);
            if (offer == null) {
                return "input";
            } else {
                return SUCCESS;
            }
        } catch (Exception ex) {
            return "input";
        }
    }

    public String validateCreate() {
        offer.setArchive(false);
        offer.setFile("");
        offer.setMaitre("");
        offer.setOwner("");
        try {
            if (beanDaoOffer.create(offer) == null) {
                return "input";
            } else {
                return SUCCESS;
            }
        } catch (Exception ex) {
            return "input";
        }
    }

    public String updateOffersList() {
        offersList = beanDaoOffer.findAll();
        Collections.sort(offersList, new BeanComparator("id"));
        return "success";
    }

    public IDaoOffer getBeanDaoOffer() {
        return beanDaoOffer;
    }

    public void setBeanDaoOffer(IDaoOffer beanDaoOffer) {
        this.beanDaoOffer = beanDaoOffer;
    }

    public List<Offer> getOffersList() {
        return offersList;
    }

    public void setOffersList(List<Offer> offersList) {
        this.offersList = offersList;
    }

    public Offer getOffer() {
        return offer;
    }

    public void setOffer(Offer offer) {
        this.offer = offer;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}
